import * as echarts from '../../lib/ec-canvas/echarts';
var api = require('../../utils/api');
var utils = require('../../utils/utils');
var range = 'M1';
var symbol = 'ftusdt';

Page({
	data: {
		ec: {
			onInit: (canvas, width, height) => {
				var chart = echarts.init(canvas, null, {
					width: width,
					height: height
				});
				canvas.setChart(chart);
				setInterval(() => {
					api.kline(range, symbol).then(kdata => {
						var data = utils.formatKline(kdata);
						var option = {
							title: {
								text: 'K 线图'
							},
							xAxis: {
								data: [] // id值的1000倍，再格式化一下
							},
							yAxis: [{
								min: 'dataMin',
								max: 'dataMax',
								axisLabel: {
									margin: 4,
								},
							}],
							tooltip: {
								trigger: 'axis',
								axisPointer: {
									type: 'cross'
								}
							},
							series: [{
									type: 'k',
									data: data,
									barWidth: '7',
									itemStyle: {
										normal: {
											color: '#09BB07',
											color0: '#F76260',
											borderColor: '#09BB07',
											borderColor0: '#F76260',
											borderWidth: 1,
											opacity: 1,
										}
									},
								},
							]
						};
						chart.setOption(option);
					});
				}, 100);
				return chart;
			}
		},
		resolutions: [{
				value: 'M1',
				title: '1分',
				checked: 'true'
			},
			{
				value: 'M3',
				title: '3分'
			},
			{
				value: 'M5',
				title: '5分'
			},
			{
				value: 'M15',
				title: '15分'
			},
			{
				value: 'H1',
				title: '1时'
			},
			{
				value: 'H4',
				title: '4时'
			},
			// {
			// 	value: 'H6',
			// 	title: '6时'
			// },
			{
				value: 'D1',
				title: '日线'
			},
			{
				value: 'W1',
				title: '周线'
			},
		],
		range: 'M1'
	},
	resolutionChange: function (e) {
		range = e.detail.value;
		this.setData({
			range: range
		});
	},
	onLoad: function (options) {
		if (options.symbol) {
			symbol = options.symbol;
		}
	}
});